package com.time.workshop;


import android.app.Application;
import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.LocationClientOption.LocationMode;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.time.workshop.utils.ScreenSizeUtil;

/**
 * 
 * @author wangfeng
 *
 */
public class App extends Application {

	private final static String TAG = "com.time.workshokp";
	public final static boolean DEBUG = true;
	public final static double imgrote = 0.40;
	public boolean isReceiveMSG = false;
	public static Context applicationContext;
	private static App mInstance = null;
	private LocationClient mLocationClient;
	public MyLocationListener mMyLocationListener;
	public String city = "";//定位城市
	public static String cityId = "";//定位城市id
	public static String county_id = "";//区县ID
	public static String street_id = "";//街道ID
	public static String county_name = "";//区县名称
	public static String street_name = "";//街道名称
	public String direction = "";//定位区县
	private int i = 0;
	public boolean isFrist = false;
	/**点击那个button进入 */
	public int type = 0;

	private ScreenSizeUtil sizeUtil;

	public ScreenSizeUtil getSizeUtil() {
		return sizeUtil;
	}

	public void setSizeUtil(ScreenSizeUtil sizeUtil) {
		this.sizeUtil = sizeUtil;
	}

	public int getScreenWidth() {
		return sizeUtil.getScreenWidth();
	}
	
	public int getScreenHeight(){
		return sizeUtil.getScreenHeight();
	}


	@Override
	public void onCreate() {
		super.onCreate();
		mInstance = this;
		sizeUtil = new ScreenSizeUtil(this);
		mLocationClient = new LocationClient(this.getApplicationContext());
		mMyLocationListener = new MyLocationListener();
		mLocationClient.registerLocationListener(mMyLocationListener);
		InitLocation();
		mLocationClient.start();
		//创建默认的ImageLoader配置参数  
        ImageLoaderConfiguration configuration = ImageLoaderConfiguration  
                .createDefault(this);  
          
        //Initialize ImageLoader with configuration.  
        ImageLoader.getInstance().init(configuration); 
	}

	/**定位参数设置*/
	private void InitLocation(){
		LocationClientOption option = new LocationClientOption();
		option.setLocationMode(LocationMode.Hight_Accuracy);//设置定位模式
		option.setCoorType("bd09ll");//返回的定位结果是百度经纬度，默认值gcj02
		int span=2000;
		option.setScanSpan(span);//设置发起定位请求的间隔时间为5000ms
		option.setIsNeedAddress(true);
		mLocationClient.setLocOption(option);
	}

	/**
	 * 实现实位回调监听
	 */
	public class MyLocationListener implements BDLocationListener {

		@Override
		public void onReceiveLocation(BDLocation location) {
			if (null == location) {
				return;
			}
			if (!isFrist) {
				city = location.getCity();
				cityId = location.getCityCode();
				direction = location.getDistrict();
				if (location.getAddrStr() != null) {
					isFrist = true;
					i = i+1;
					System.out.println("第"+i+"次，地址："+location.getAddrStr());
				}
			}
		}
	}



	public static App getInstance() {

		if(mInstance == null ){
			mInstance = new App();
		}
		return mInstance;

	}
	//--Toast--------------------------------
	private static Handler mHandler = new Handler();
	private static Toast toast;
	public static void showShortToast(final CharSequence msg) {
		mHandler.post(new Runnable() {

			@Override
			public void run() {
				if (toast == null)
					toast = Toast.makeText(mInstance, msg, Toast.LENGTH_SHORT);
				else
					toast.setText(msg);

				toast.show();
			}
		});
	}
	public static void showShortToast(int resId) {
		showShortToast(mInstance.getString(resId));
	}

	/**
	 *打印Logs
	 * 
	 * @param msg
	 */
	public static void showLog(Object msg) {
		if (DEBUG)
			Log.i("BAO", msg + "");
	}

	public static void debug(String tag, Object msg) {
		if (DEBUG)
			Log.d(TextUtils.isEmpty(tag) ? TAG : tag, String.valueOf(msg));
	}

	public static void debug(Object msg) {
		debug(null, msg);
	}
}
